import 'package:flutter/material.dart';
import 'package:flutter_09_game/generated/locales.g.dart';
import 'package:flutter_09_game/utils/loading/loading_utils.dart';
import 'package:flutter_09_game/utils/log/utils_log.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get_utils/get_utils.dart';

import '../../config/app_color.dart';

/// 项目中通用确认按钮
/// 根据 Figma 设计稿实现，支持启用/禁用两种状态
class CommonButton extends StatelessWidget {
  final String title;
  final Function()? onClick;
  final Future Function()? onAsyncClick;
  final double? height;
  final double? width;
  final double? fontSize;
  final FontWeight? fontWeight;
  final Color? textColor;
  final double horizontalPadding;
  final double verticalPadding;
  final bool enable;
  final List<Color>? backgroundColors;
  final List<Color>? disableBackgroundColors;
  final bool alwaysCanClick;
  final BorderRadius? borderRadius;
  final Border? border;
  final Duration? delayed;
  final double? paddingBottom;
  final double? elevation;

  // 最小宽度按钮
  CommonButton({
    super.key,
    required this.title,
    this.onClick,
    this.enable = true,
    this.height,
    this.width,
    this.fontSize,
    this.fontWeight,
    this.textColor,
    this.horizontalPadding = 12.0,
    this.verticalPadding = 0.0,
    this.backgroundColors,
    this.disableBackgroundColors,
    this.alwaysCanClick = false,
    this.borderRadius,
    this.border,
    this.onAsyncClick,
    this.delayed = const Duration(milliseconds: 250),
    this.paddingBottom,
    this.elevation,
  });

  // 最大宽度按钮
  CommonButton.expend({
    super.key,
    required this.title,
    this.onClick,
    this.enable = true,
    this.height,
    this.width = double.infinity,
    this.fontSize,
    this.fontWeight,
    this.textColor,
    this.horizontalPadding = 12.0,
    this.verticalPadding = 0.0,
    this.backgroundColors,
    this.disableBackgroundColors,
    this.alwaysCanClick = false,
    this.borderRadius,
    this.border,
    this.onAsyncClick,
    this.delayed = const Duration(milliseconds: 250),
    this.paddingBottom,
    this.elevation,
  });

  bool _isTapped = false;

  @override
  Widget build(BuildContext context) {
    // 根据设计稿，圆角为 40px，换算为屏幕适配单位
    final effectiveBorderRadius = borderRadius ?? BorderRadius.circular(8.w);

    // 根据设计稿，高度为 48px
    final effectiveHeight = height ?? 52.w;

    // 按钮背景色：启用状态使用主色，禁用状态使用浅色
    List<Color> buttonColors = enable
        ? (backgroundColors ?? AppColor.btnSelGradient)
        : (disableBackgroundColors ?? AppColor.btnNorGradient);
    // clog('message  $enable ${buttonColors.toString()}');
    // 文字颜色：两种状态都使用白色
    final buttonTextColor =
        textColor ?? AppColor.textPrimary.withOpacity(enable ? 0.94 : 0.7);

    // 字体样式：Outfit Medium 16px
    final effectiveFontSize = fontSize ?? 16.sp;
    final effectiveFontWeight = fontWeight ?? FontWeight.w500;

    return GestureDetector(
      onTap: (alwaysCanClick || enable) ? _onTap : null,
      child: Container(
        height: effectiveHeight,
        width: width,
        decoration: BoxDecoration(
            // color: Colors.red,
            borderRadius: effectiveBorderRadius,
            border: border ??
                const Border(
                    bottom:
                        BorderSide(color: AppColor.btnBottomBorder, width: 2)),
            gradient: LinearGradient(
                colors: buttonColors,
                begin: Alignment.centerLeft,
                end: Alignment.centerRight)),
        padding: EdgeInsets.symmetric(
          horizontal: horizontalPadding,
          vertical: verticalPadding,
        ),
        child: Center(
          child: Padding(
            padding: EdgeInsets.only(bottom: paddingBottom ?? 0),
            child: Text(
              title,
              style: TextStyle(
                color: buttonTextColor,
                fontSize: effectiveFontSize,
                fontWeight: effectiveFontWeight,
                // height: 1.26,
                leadingDistribution: TextLeadingDistribution.even,
              ),
              textAlign: TextAlign.center,
            ),
          ),
        ),
      ),
    );
  }

  _onTap() {
    FocusManager.instance.primaryFocus?.unfocus();
    if (!_isTapped) {
      _isTapped = true;
      if (onClick != null) {
        _tapWithOnTap();
      } else if (onAsyncClick != null) {
        _tapWithAsyncTap();
      }
    } else {
      if (!LoadingUtils.isShow) {
        LoadingUtils.showToast(LocaleKeys.operationTooFrequent.tr);
      }
    }
  }

  void _tapWithAsyncTap() async {
    await onAsyncClick?.call();
    _isTapped = false;
  }

  void _tapWithOnTap() {
    onClick?.call();
    if (delayed == null) {
      _isTapped = false;
    } else {
      Future.delayed(delayed!, () {
        _isTapped = false;
      });
    }
  }
}
